package com.yuanheng100.ems4j.mapper;

import com.yuanheng100.ems4j.model.Ems4jEmail;
import com.yuanheng100.ems4j.model.Ems4jPage;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;
import java.util.Map;

public interface Ems4jEmailMapper
{
    @Insert({
            "INSERT INTO ems4j_email (emailId, sender, ",
            "content, receiver, ",
            "fromName, toName, ",
            "emailSubject, sendTime, ",
            "result)",
            "VALUES (#{emailId,jdbcType=INTEGER}, #{sender,jdbcType=VARCHAR}, ",
            "#{content,jdbcType=VARCHAR}, #{receiver,jdbcType=VARCHAR}, ",
            "#{fromName,jdbcType=VARCHAR}, #{toName,jdbcType=VARCHAR}, ",
            "#{emailSubject,jdbcType=VARCHAR}, #{sendTime,jdbcType=TIMESTAMP}, ",
            "#{result,jdbcType=SMALLINT})"
    })
    @Options(useGeneratedKeys = true, keyProperty = "emailId")
    int insert(Ems4jEmail record);

    @InsertProvider(type = Ems4jEmailSqlProvider.class, method = "insertSelective")
    int insertSelective(Ems4jEmail record);

    @Select({
            "SELECT",
            "emailId, sender, content, receiver, fromName, toName, emailSubject, sendTime, ",
            "result",
            "FROM ems4j_email",
            "WHERE emailId = #{emailId,jdbcType=INTEGER}"
    })
    @Results({
            @Result(column = "emailId", property = "emailId", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "sender", property = "sender", jdbcType = JdbcType.VARCHAR),
            @Result(column = "content", property = "content", jdbcType = JdbcType.VARCHAR),
            @Result(column = "receiver", property = "receiver", jdbcType = JdbcType.VARCHAR),
            @Result(column = "fromName", property = "fromName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "toName", property = "toName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "emailSubject", property = "emailSubject", jdbcType = JdbcType.VARCHAR),
            @Result(column = "sendTime", property = "sendTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "result", property = "result", jdbcType = JdbcType.SMALLINT)
    })
    Ems4jEmail selectByPrimaryKey(Integer emailId);

    @UpdateProvider(type = Ems4jEmailSqlProvider.class, method = "updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(Ems4jEmail record);

    @Update({
            "UPDATE ems4j_email",
            "SET sendTime = #{sendTime,jdbcType=TIMESTAMP},",
            "result = #{result,jdbcType=SMALLINT}",
            "WHERE emailId = #{emailId,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(Ems4jEmail record);

    @SelectProvider(type = Ems4jEmailSqlProvider.class, method = "selectCountByCondition")
    Long selectCountByCondition(Map<String, Object> conditions);

    @SelectProvider(type = Ems4jEmailSqlProvider.class, method = "selectListByCondition")
    List<Ems4jEmail> selectListByCondition(@Param("conditions") Map<String, Object> conditions, @Param("page") Ems4jPage<Ems4jEmail> page);
}